﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using SpaceShooter.Components;
using SpaceShooter.Common;

namespace SpaceShooter.GameObjects
{
    public class MissileLauncher
    {
        private IGameScreen gameScreen;
        private TimeSpan remainingTimeToNextShot;
        private TimeSpan remainingTimeToNextManualShot;
        private GameObject launchingObject;
        private bool shootingEnabled = false;
        private Func<bool> launchAction;

        public MissileLauncher(IGameScreen gameScreen, GameObject launchingObject, Func<bool> launchAction)
        {
            this.gameScreen = gameScreen;
            this.launchingObject = launchingObject;
            this.launchAction = launchAction;
            this.TimeBetweenShots = TimeSpan.Zero;
            this.MinTimeBetweenShots = TimeSpan.Zero;
            this.remainingTimeToNextManualShot = TimeSpan.Zero;
            this.LaunchOffset = Vector3.Zero;
        }

        public TimeSpan TimeBetweenShots { get; set; }

        public TimeSpan MinTimeBetweenShots { get; set; }

        public Vector3 LaunchOffset { get; set; }

        public void Update(GameTime gameTime)
        {
            if (this.shootingEnabled)
            {
                // spawn new missiles
                if (remainingTimeToNextShot == TimeSpan.MinValue)
                {
                    this.SpawnMissile();
                    this.remainingTimeToNextShot = this.TimeBetweenShots;
                }
                else
                {
                    this.remainingTimeToNextShot -= gameTime.ElapsedGameTime;

                    if (this.remainingTimeToNextShot < TimeSpan.Zero)
                    {
                        this.SpawnMissile();
                        this.remainingTimeToNextShot += this.TimeBetweenShots;
                    }
                }
            }

            this.remainingTimeToNextManualShot -= gameTime.ElapsedGameTime;
        }

        private void SpawnMissile()
        {
            if (this.launchAction())
            {
                this.SpawnRocket();
            }
        }

        private void SpawnRocket()
        {
            var go = new RocketMissile(this.gameScreen, TimeSpan.FromSeconds(3));

            go.SetDirection(Vector3.Forward);
            go.SetPosition(this.launchingObject.Position + this.LaunchOffset);

            MissileManagerComponent.AddMissile(go);
            CollisionManager.AddObject(go);
        }

        public bool ShootingEnabled
        {
            get { return this.shootingEnabled; }
            set
            {
                this.shootingEnabled = value;
            }
        }

        public void Launch()
        {
            if (this.remainingTimeToNextManualShot < TimeSpan.Zero)
            {
                this.SpawnMissile();
                this.remainingTimeToNextManualShot = this.MinTimeBetweenShots;
            }
        }
    }
}
